<template>
	<view>
		<u-navbar :is-back="false" title="" :background="{backgroundColor:'#f1f1f1'}">
			<view class="custom-navbar">
				<view class="nav-left" @click="customBack">
					<view>{{pageStatus==0?'关闭':'取消'}}</view>
				</view>
				<view class="nav-center u-flex u-row-center u-col-center">
					<view>{{pageStatus==1?'选择多个聊天':'选择一个聊天'}}</view>
				</view>
				<view class="nav-right">
					<template v-if="pageStatus==1">
						<u-button v-if="selectedGroups.length>0" @click="toBulkForward()" size="mini" type="success">
							完成({{selectedGroups.length}}/9)</u-button>
						<u-button v-if="selectedGroups.length<1" size="mini" :disabled="true" type="default">完成
						</u-button>
					</template>
					<template v-else>
						<view style="text-align: right;" @click="pageStatus = 1">多选</view>
					</template>
				</view>
			</view>
		</u-navbar>
		<view v-if="myGroupList.length>0">
			<!-- <view class="u-flex u-row-between u-col-center u-font-28  u-border-bottom" style="padding: 30rpx 20rpx;">
				<view style="color: #000000;font-weight: bold;">最近聊天</view>
				<view @click="toCreateNewChatgroup">
					<u-icon name="plus" label="创建新的聊天" label-color="#36648b" label-pos="right" margin-left="12rpx"
						color="#36648b"></u-icon>
				</view>
			</view> -->
			<scroll-view :scroll-x="false" :scroll-y="true" :style="'height:'+scrollViewHeight+'px'"
				class="hidden-scroll-bar">
				<view v-for="(group,index) in myGroupList" :key="index"
					class="u-flex u-row-left u-col-center u-border-bottom">
					<!-- v-if="group.id!=currentChatGroupId"-->
					<template>
						<view v-if="pageStatus==1"
							style="text-align: center;width: 50rpx;margin-right: 16rpx;margin-left: 16rpx;"
							@click="changeStatus(group,index)">
							<view v-show="group.selected==true">
								<image style="width: 40rpx;height: 40rpx;" mode="aspectFill"
									src="@/static/image/fileType/redioCheck.png"></image>
							</view>
							<view v-show="group.selected==false">
								<image style="width: 40rpx;height: 40rpx;" mode="aspectFill"
									src="@/static/image/fileType/radioNoCheck.png"></image>
							</view>
						</view>
						<view :style="pageStatus==1?'width: 90%;':'width:100%'">
							<u-cell-item @click="toForward(group, index)" :title="group.group.title"
								:title-style="{fontSize:'32rpx',color:'#000000'}" :arrow="false" :border-bottom="false">
								<view slot="icon" style="border-radius: 10rpx;" class="u-m-r-16">
									<static-image :src="group.group.cover" :width="90" :height="90" :radius="10">
									</static-image>
								</view>
								<view slot="label">
									<!-- <text>{{group.memberCounts}}人</text> -->
								</view>
							</u-cell-item>
						</view>
					</template>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentChatGroupId: '',
				pageStatus: 0,
				scrollViewHeight: 0,
				sendType: 1,
				delta:1,
				msgList: {},
				myGroupList: [],
				selectedGroups: [],
			};
		},
		created: function() {
			this.scrollViewHeight = uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight -
			98;
		},
		onLoad: function(option) {
			if (option.sendType) {
				this.sendType = Number(option.sendType);
			}
			if (option.delta){
				this.delta=Number(option.delta);
			}
			if (option.fromChatGroupId) {
				this.currentChatGroupId = Number(option.fromChatGroupId);
			}
			let msgList = option.msgList;
      this.msgList = JSON.parse(decodeURIComponent(msgList)).map(s => {
        const content = s.content
        delete content.m
        return {
          content_type: s.content_type,
          type: 'USER',
          user_id: this.vuex_userInfo.id,
          content
        }
      });
				//this.msgList = JSON.parse(msgList);
				//console.log("要转发的信息", this.msgList);
			this.getMyChatGroupList();
		},
		methods: {
			//自定义返回
			customBack: function() {
				if (this.pageStatus == 0) {
					uni.navigateBack();
				} else {
					this.pageStatus = 0;
					this.selectedGroups = [];
					this.myGroupList.forEach(function(value, index) {
						value.selected = false;
					})
				}
			},
			//获取我的群组列表
			getMyChatGroupList () {
        this.myGroupList = this.vuex_groupList.filter(s => s.group.is_multi !== 'SYSTEM' && s.status !== 'REJECT').map(s => ({
          selected: false,
          disabled: false,
          ...s
        }))
			},
			//点击群组转发时间
			toForward: function(group, index) {
				if (this.pageStatus == 1) {
					this.changeStatus(group, index)
				} else {
					this.forwardMsg({
            group_id: group.group_id
          });
				}
			},
      async toBulkForward () {
        console.log(this.selectedGroups)
        for (const group of this.selectedGroups) {
          this.forwardMsg({
            group_id: group.group_id
          }, true)
        }
        this.$u.toast('转发成功！', 1000)
        setTimeout(() => {
          uni.getStorageSync('need-loadNowMessage', true)
          if(this.delta==2){
            //多选的直接退到聊天界面
            uni.navigateBack({
              delta:2,
            })
          }else{
            uni.navigateBack();
          }
        }, 1100)
      },
			//转发信息
			async forwardMsg (param, silent = false) {
        const msg = this.msgList[0]
        const res = await this.$u.api.chatGroup.sendMsg({
          ...param,
          ...msg
        });
        if (res.code !== 200) return
        if (silent) return
        this.$u.toast('转发成功！', 1000)
        setTimeout(() => {
          uni.getStorageSync('need-loadNowMessage', true)
          if(this.delta==2){
            //多选的直接退到聊天界面
            uni.navigateBack({
              delta:2,
            })
          }else{
            uni.navigateBack();
          }
        }, 1100)
			},
			//选择聊天中的文件
			changeStatus: function(groupObj, key) {
				let that = this;
				let index;
				if (that.selectedGroups.length >= 9) {
					index = that.selectedGroups.findIndex(function(obj) {
						return obj.id === groupObj.id
					})
				}
				if (index === -1) {
					this.globalUtil.utilAlert("最多能勾选9个", 'warning');
					return;
				}
				that.myGroupList[key].selected = !this.myGroupList[key].selected;
				//如果选中的列表为空,并且当前点击的文件selected等于true,就放入已选择的列表中
				if (that.selectedGroups.length === 0) {
					if (groupObj.selected === true) {
						that.selectedGroups.push(groupObj);
					}
				} else {
					let j = -1;
					for (let i = 0; i < that.selectedGroups.length; i++) {
						if (groupObj.id === that.selectedGroups[i].id) {
							//当存在，再次点击说明需要取消选中
							if (groupObj.selected === false) {
								//说明是选中的状态
								j = i;
								break;
							}
						}
					}
					if (j !== -1) {
						//表示先获取这个元素的下标，然后从这个下标开始计算，删除长度为1的元素
						that.selectedGroups.splice(j, 1);
					} else {
						that.selectedGroups.push(groupObj);
					}
				}
			},

			toCreateNewChatgroup: function() {
				let that=this;
				console.log("发起新的群聊进行转发");
				this.$u.route({
					url: '/pages/chat/chatGroup/forwardMsgToNewGroup',
					params: {
						messageList:JSON.stringify(that.msgList),
						sendType: that.sendType,
						delta:3,
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.custom-navbar {
		width: 100%;
		display: flex;
		flex: 1;
		flex-direction: row;
		justify-content: space-between;
		align-content: center;
		align-items: center;
		padding: 0 30rpx;

		.nav-left {
			font-size: 32rpx;
			color: #333333;
			width: 15%;
		}

		.nav-center {
			font-size: 36rpx;
			font-weight: bold;
			width: 65%;
		}

		.nav-right {
			font-size: 32rpx;
			color: #333333;
			width: 16%;
			text-align: right;
		}
	}

	.hidden-scroll-bar {
		::-webkit-scrollbar {
			display: none;
			height: 0;
			width: 0;
		}
	}
</style>
